﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Table_pet.background;
using Table_pet.background.Classes;
using static System.Windows.Forms.ListViewItem;

namespace Table_pet
{
    public partial class MemorandumList : Form
    {
        List<Memorandum> list = new List<Memorandum>();
        public MemorandumList()
        {
            InitializeComponent();
            listView1.Items.Clear();
            list = DB_api.readAllMemorandum();
            foreach (Memorandum m in list)
            {
                ListViewSubItem listViewSubItem0 = new ListViewSubItem();
                ListViewSubItem listViewSubItem1 = new ListViewSubItem();
                ListViewSubItem listViewSubItem2= new ListViewSubItem();
                listViewSubItem0.Text = m.date.ToString();
                listViewSubItem1.Text = m.content;
                listViewSubItem2.Text = m.Id + "";
                ListViewItem listViewItem = new ListViewItem();
                listViewItem.SubItems.Add(listViewSubItem0);
                listViewItem.SubItems.Add(listViewSubItem1);
                listViewItem.SubItems.Add(listViewSubItem2);
                listViewItem.ImageIndex = 0;
                listViewItem.Text = "";
                listView1.Items.Add(listViewItem);
            }


            label1.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*list.Add(new Memorandum(new DateTime(2023, 1, 1), "one", 0));
            list.Add(new Memorandum(new DateTime(2023, 2, 2), "two", 0));
            list.Add(new Memorandum(new DateTime(2023, 3, 3), "three", 1));
            foreach (Memorandum m in list)
            {
                DB_api.AddMemorandumDate(m);
                ListViewSubItem listViewSubItem0 = new ListViewSubItem();
                ListViewSubItem listViewSubItem1 = new ListViewSubItem();
                listViewSubItem0.Text = m.date.ToString();
                listViewSubItem1.Text = m.content;
                ListViewItem listViewItem = new ListViewItem();
                listViewItem.SubItems.Add(listViewSubItem0);
                listViewItem.SubItems.Add(listViewSubItem1);
                listViewItem.ImageIndex = 0;
                listViewItem.Text = m.Id + "";
                listView1.Items.Add(listViewItem);
            }/*/
            AddMemorandumList addMemorandumList = new AddMemorandumList(this.Location);
            addMemorandumList.ShowDialog();

            listView1.Items.Clear();
            list = DB_api.readAllMemorandum();
            foreach (Memorandum m in list)
            {
                ListViewSubItem listViewSubItem0 = new ListViewSubItem();
                ListViewSubItem listViewSubItem1 = new ListViewSubItem();
                ListViewSubItem listViewSubItem2= new ListViewSubItem();
                listViewSubItem0.Text = m.date.ToString();
                listViewSubItem1.Text = m.content;
                listViewSubItem2.Text = m.Id + "";
                ListViewItem listViewItem = new ListViewItem();
                listViewItem.SubItems.Add(listViewSubItem0);
                listViewItem.SubItems.Add(listViewSubItem1);
                listViewItem.SubItems.Add(listViewSubItem2);
                listViewItem.ImageIndex = 0;
                listViewItem.Text = "";
                listView1.Items.Add(listViewItem);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ListView.CheckedListViewItemCollection checkedItems = listView1.CheckedItems;
            if (checkedItems.Count != 1)
            {
                label1.Visible = true;
            }
            else
            {
                label1.Visible = false;
                foreach (ListViewItem item in checkedItems)
                {
                    int id = int.Parse(item.SubItems[3].Text);
                    Memorandum m = DB_api.searchMemorandumById(id);
                    DB_api.deleteMemorandumById(id);
                    AddMemorandumList addMemorandumList = new AddMemorandumList(this.Location, m);
                    addMemorandumList.ShowDialog();

                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ListView.CheckedListViewItemCollection checkedItems = listView1.CheckedItems;
            foreach (ListViewItem item in checkedItems)
            {
                listView1.Items.Remove(item);
                int id = int.Parse(item.SubItems[3].Text);
                DB_api.deleteMemorandumById(id);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MemorandumList_Load(object sender, EventArgs e)
        {
            ControlBox = true;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
